package com.csmht.tododemo.service.impl;

import com.csmht.tododemo.constant.MessageConstant;
import com.csmht.tododemo.constant.RedisKeyConstant;
import com.csmht.tododemo.exiception.BaseException;
import com.csmht.tododemo.mapper.ClassMapper;
import com.csmht.tododemo.mapper.TodoClassMapper;
import com.csmht.tododemo.mapper.TodoMapper;
import com.csmht.tododemo.pojo.entity.Todo;
import com.csmht.tododemo.pojo.entity.TodoClass;
import com.csmht.tododemo.pojo.entity.TodoWhitClass;
import com.csmht.tododemo.pojo.vo.ToDoVO;
import com.csmht.tododemo.until.VisitorUrlUntil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class VisitorServiceImpl implements com.csmht.tododemo.service.VisitorService {


    @Resource
    private TodoMapper todoMapper;


    @Resource
    private ClassMapper classMapper;

    @Override
    public ToDoVO getByKey(String key) {

        log.info("getByKey key:{}", key);

        String todoId = VisitorUrlUntil.decrypt(key);

        Todo todo = todoMapper.selectById(Long.valueOf(todoId));

        if(todo == null || todo.getVisitor() == 0){
            throw new BaseException(MessageConstant.NO_SHARE);
        }

        ToDoVO toDoVO = ToDoVO.builder().build();

        BeanUtils.copyProperties(todo,toDoVO);

        List<TodoClass> todoClasses = classMapper.selectByToDoId(Long.valueOf(todoId));

        toDoVO.setClasss(todoClasses);

        return toDoVO;
    }
}
